<template>
  <div class="register">
    <form>
      <div class="label">
        <label class="error">{{ message }}</label>
        <input
          name="username"
          type="text"
          v-model="username"
          placeholder="请输入用户名"
        />
        <input
          type="password"
          v-model.trim="password"
          placeholder="请输入密码"
        />
        <input
          type="password"
          v-model.trim="password2"
          placeholder="请输入确认密码"
        />
        <input type="tel" v-model.trim="mobile" placeholder="请输入手机号" />
      </div>
      <div class="submit">
        <input type="submit" @click.prevent="register" value="注册" />
      </div>
    </form>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
export default {
  name: "UserRegister",
  props: [""],
  data() {
    return {
      username: "",
      password: "",
      password2: "",
      mobile: "",
      message: "",
    };
  },
  watch: {
    username(newVal) {
      // 取消上一次请求
      if (newVal) {
        this.cancelRequest();
        this.axios
          .get("/user/" + newVal, {
            cancelToken: new this.axios.CancelToken(
              (cancel) => (this.cancel = cancel)
            ),
          })
          .then((response) => {
            if (response.data.code == 200) {
              let isExist = response.data.data;
              if (isExist) {
                this.message = "该用户名已经存在";
              } else {
                this.message = "";
              }
            }
          })
          .catch((error) => {
            if (this.axios.isCancel(error)) {
              // 如果是请求被取消产生的错误，输出取消请求得原因
              console.log("请求取消:", error.message);
            } else {
              console.log(error);
            }
          });
      }
    },
  },
  methods: {
    ...mapMutations("user", ["saveUser"]),
    cancelRequest() {
      if (typeof this.cancel === "function") {
        this.cancel("终止请求");
      }
    },
    checkForm() {
      if (!this.username || !this.password || !this.password2 || !this.mobile) {
        this.message = "所有字段不能为空";
        return false;
      }
      if (this.password !== this.password2) {
        this.message = "密码和确认密码必须相同";
        return false;
      }
      return true;
    },
    register() {
      this.message = "";
      if (!this.checkForm()) return;
      this.axios
        .post("/user/register", {
          username: this.username,
          password: this.password,
          mobile: this.mobile,
        })
        .then((response) => {
          if (response.data.code === 200) {
            this.saveUser(response.data.data);
            this.username = "";
            this.password = "";
            this.password2 = "";
            this.mobile = "";
            this.$router.push("/");
          } else if (response.data.code === 500) {
            this.message = "用户注册失败";
          }
        })
        .catch((error) => {
          alert(error.message);
        });
    },
  },
};
</script>

<style scoped>
.register {
  margin: 50px auto;
  width: 380px;
}

.register input {
  padding: 20px;
  width: 90%;
  font-size: 20px;
  margin: 18px 0px;
  color: rgb(78, 77, 77);
  float: left;
  cursor: pointer;
  outline: none;
  background: rgba(240, 235, 235, 0.438);
  transition: all 0.3s ease-out;
  border: solid 1px rgb(161, 161, 161);
  border-radius: 30px;
  transition: 2s;
}

.register input:hover {
  color: rgb(50, 104, 167);
  background: rgba(116, 176, 255, 0.753);
}

.register .submit {
  text-align: center;
}
.register input[type="submit"] {
  padding: 17px 17px;
  color: #fff;
  background: #3a8efd;
  border: solid 1px #3a8efd;
  cursor: pointer;
  font-size: 18px;
  transition: all 0.5s ease-out;
  outline: none;
  width: 100%;
}

.register .submit input[type="submit"]:hover {
  background: #1546cc;
  border: solid 1px #1546cc;
}
.register .error {
  color: red;
  font-weight: bold;
}
</style>
